/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2019-12-27
 * Time: 17:04
 */
#include "TabWidget.h"
#include "Ui_TabWidget.h"



TabWidget::TabWidget(QWidget *parent) :
        QTabWidget(parent), ui(new Ui::TabWidget) {
    ui->setupUi(this);
    this->resize(256,273);

    myTime = new MyTimeHelper();


    int x = parent->width()/2 - this->width()/2;
    int y = parent->height()/2 - this->height()/2;
    this->move(x,y);
    this->setStyle("../res/css/tabWidget.css");

    //登录验证
    connect(ui->login_submit,SIGNAL(clicked()),this,SLOT(isLogin()));
    //注册验证
    connect(ui->sign_submit,SIGNAL(clicked()),this,SLOT(isSign()));
}


TabWidget::~TabWidget() {

}

void TabWidget::setStyle(const QString &styleName) {
    QFile file(QString("%1").arg(styleName));
    file.open(QFile::ReadOnly);
    QString css = QLatin1String(file.readAll());
    qApp->setStyleSheet(css);

    int width = this->width();
    int tabCount = this->count();
    int tabWidth = width / tabCount;
    //qDebug()<<"tabwidth:"<<tabWidth;
}

bool TabWidget::isLogin() {
    QString username = ui->login_username_input->text();
    QString passwd = ui->login_passwd_input->text();

    user.setName(username);
    user.setPasswd(passwd);

    if(userDao.checkUserInfo(user)){    //检查用户名是否存在
        if (userDao.checkPasswdInfo(user)){
            qDebug()<<"login success";
            QMessageBox::information(this,"提示","登录成功",QMessageBox::QMessageBox::Yes);
            emit showMainForm();
            //ui->login_username_input->clear();
            ui->login_passwd_input->clear();
        }else{
            qDebug()<<"passwd don't same";
            QMessageBox::information(this,"提示","密码不正确",QMessageBox::QMessageBox::Yes);
            ui->login_passwd_input->clear();
        }
    } else{
        qDebug()<<"please sign up";
        QMessageBox::information(this,"提示","账号不存在，请先注册",QMessageBox::QMessageBox::Yes);
        ui->login_username_input->clear();
        ui->login_passwd_input->clear();
    }
    return false;
}

bool TabWidget::isSign() {
    QString username = ui->sign_username_input->text();
    QString passwd = ui->sign_passwd_input->text();
    QString passwd2 = ui->sign_passwd2_input->text();

    if (passwd == passwd2){
        //qDebug()<<"密码相同\n";
        user.setName(username);
        user.setPasswd(passwd);
        user.setSignTime(myTime->getTimeStr());
        user.setLevel(1);

        if (userDao.checkUserInfo(user)){
            //输入密码一致时 ，检测用户是否存在
            QMessageBox::information(this,"提示","名字已存在，请登录!",QMessageBox::QMessageBox::Yes);
            qDebug()<<"name exit! please Login\n";
            ui->sign_username_input->clear();
            ui->sign_passwd_input->clear();
            ui->sign_passwd2_input->clear();
        } else{
            //密码一致，用户名不存在则存入数据库，并提示用户注册成功。
            if (userDao.save(user)){
                qDebug()<<"Sign success!\n";
                QMessageBox::information(this,"提示","注册成功",QMessageBox::QMessageBox::Yes);
                ui->sign_username_input->clear();
                ui->sign_passwd_input->clear();
                ui->sign_passwd2_input->clear();
            } else{
                qDebug()<<"insert failed!\n";
                ui->sign_username_input->clear();
                ui->sign_passwd_input->clear();
                ui->sign_passwd2_input->clear();
            }
        }
    } else{
        //密码不一致
        qDebug()<<"密码不一致\n";
        QMessageBox::information(this,"提示","密码不一致",QMessageBox::QMessageBox::Yes);
        //ui->sign_username_input->clear();
        ui->sign_passwd_input->clear();
        ui->sign_passwd2_input->clear();
    }
    return false;
}
